using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 冲刺Buff - 快速移动到指定位置
/// </summary>
public class DashBuff : BuffBase
{
    private Vector3 m_DashDirection;
    private float m_DashSpeed;
    private float m_DashDistance;
    private bool m_IsDashing;
    private Vector3 m_StartPosition;
    private Vector3 m_TargetPosition;

    public DashBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is DashData dashData)
        {
            m_DashSpeed = dashData.DashSpeed;
            m_DashDistance = dashData.DashDistance;
        }
        else
        {
            m_DashSpeed = 15.0f; // 默认冲刺速度
            m_DashDistance = 5.0f; // 默认冲刺距离
        }
        
        // 计算冲刺方向
        if (Caster != null)
        {
            m_DashDirection = (Caster.transform.position - Target.transform.position).normalized;
        }
        else
        {
            m_DashDirection = Vector3.right; // 默认向右冲刺
        }
        
        m_StartPosition = Target.transform.position;
        m_TargetPosition = m_StartPosition + m_DashDirection * m_DashDistance;
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        m_IsDashing = true;
        
        // 播放冲刺特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("dashing", true);
        }
        
        Logger.Log($"{Target.name} 开始冲���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        if (m_IsDashing)
        {
            // 执行冲刺移动
            Vector3 currentPos = Target.transform.position;
            Vector3 newPos = Vector3.MoveTowards(currentPos, m_TargetPosition, m_DashSpeed * dt);
            Target.transform.position = newPos;
            
            // 检查是否到达目标位���
            if (Vector3.Distance(newPos, m_TargetPosition) < 0.1f)
            {
                m_IsDashing = false;
                Target.transform.position = m_TargetPosition;
            }
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        m_IsDashing = false;
        Logger.Log($"{Target.name} 冲刺结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        m_IsDashing = false;
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("dashing", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Dash"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 冲刺效果不能叠加，只能刷新持续时���
    }
}
